
/**axios封装
 * 请求拦截、相应拦截、错误统一处理
 * https://juejin.im/post/5b55c118f265da0f6f1aa354
 */
import axios from 'axios';
import { Notification, MessageBox, Message } from 'element-ui';
import { getStore } from "@/config/mUtils.js";
import { logout } from '@/api/getData';
import qs from 'qs';
import common from '@/api/api';
import router from '../router'

// axios 配置
axios.defaults.timeout = 60000;   //60s
//请求头
axios.defaults.baseURL = '/app'

// http request 拦截器
axios.interceptors.request.use(
  config => {
  	const token = getStore('bossAppToken');
    const requestEnvironment = (common.apihost=='https://qzsyapi.qianzhiqd.com'?'prod':'test')
    if (token && config.url.indexOf('demo-vod.cn-shanghai.aliyuncs.com')=='-1') {
    	config.headers['AuthorizationMega'] = token;
      config.headers['requestEnvironment'] = requestEnvironment;
    }
    return config
  },
  err => {
    return Promise.reject(err)
  },
)

// 响应拦截（配置请求回来的信息）
axios.interceptors.response.use(function (response){
 		if(response.data.code == 401){
 			MessageBox.confirm('登录状态已过期，您可以继续留在该页面，或者重新登录', '系统提示', {
          confirmButtonText: '重新登录',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        logout().then(() => {})
        location.href = '/login';
      }).catch(() => {});
 		}else if(response.data.code == 500){
 			// Message({
    //     message: '接口异常',
    //     type: 'error'
    //   })
 		}
 　　return response;
}, function (error){
 　　return Promise.reject(error);
 });

export default axios
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params){
    return new Promise((resolve, reject) =>{
        axios.get(common.apihost+url, {
            params: params
        })
        .then(res => {
            resolve(res.data);
        })
        .catch(err => {
            reject(err.data)
        })
    })
}
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(common.apihost+url, params)
        .then(res => {
            resolve(res.data);
        })
        .catch(err => {
            reject(err.data)
        })
    })
}

export function postForm(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(common.apihost+url, qs.stringify(params))
        .then(res => {
            resolve(res.data);
        })
        .catch(err => {
            reject(err.data)
        })
    })
}

export function put(url, params) {
    return new Promise((resolve, reject) => {
        axios.put(common.apihost+url, params)
        .then(res => {
            resolve(res.data);
        })
        .catch(err => {
            reject(err.data)
        })
    })
}


export function del(url, params) {
    return new Promise((resolve, reject) => {
        axios.delete(common.apihost+url, params)
        .then(res => {
            resolve(res.data);
        })
        .catch(err => {
            reject(err.data)
        })
    })
}
